<template>
	<view>
		<uni-label class="switch" style="width: width">
			<uni-view class="toggle"></uni-view>
			<uni-view class="names">
				<view :animation="animationData" class="css_sty">
				</view>
				<view class="left" :style="{
            color: current == false ? 'rgb(0, 0, 0)' : 'rgb(118, 118, 118)',
          }" @click="leftButton('left')">{{ left }}</view>
				<view class="right" @click="rightButton('right')" :style="{
            color: current == true ? 'rgb(0, 0, 0)' : 'rgb(120, 120, 120)',
          }">{{ right }}</view>
			</uni-view>
		</uni-label>
	</view>
</template>

<script>
	export default {
		name: "mimicry-switch",
		data() {
			return {
				animationData: {},
				thiscuren: Boolean,
			};
		},
		props: {
			left: {
				type: String,
				default: "关闭",
			},
			right: {
				type: String,
				default: "打开",
			},

			//指当前状态
			current: {
				type: Boolean,
				default: true,
			},
		},
		mounted() {
			if (this.current) {
				this.clickright();
			} else {
				this.clickleft();
			}
		},
		methods: {
			leftButton(e) {
				console.log(e);
				if (e == "left" && this.current == true) {
					this.clickleft();
				}
				console.log(this.current);
			},
			rightButton(e) {
				console.log(e);
				if (e == "right" && this.current == false) {
					this.clickright();
					console.log(this.current);
				}
			},
			clickleft() {
				var animation = uni.createAnimation({
					duration: 100,
					timingFunction: "ease",
				});
				this.animation = animation;
				this.animationData = animation.export();

				setTimeout(
					function() {
						animation.translate(3).step();
						this.animationData = animation.export();
					}.bind(this),
					50
				);
				this.$emit("update:current", false);
			},
			clickright() {
				var animation = uni.createAnimation({
					duration: 100,
					timingFunction: "ease",
				});
				this.animation = animation;
				this.animationData = animation.export();
				setTimeout(
					function() {
						animation.translate(55).step();
						this.animationData = animation.export();
					}.bind(this),
					50
				);
				this.$emit("update:current", true);
			},
		},
	};
</script>

<style lang="less">
	.switch {
		width: 220rpx;
		height: 56rpx;
		background: #F7F7F7;
		border-radius: 28rpx;
		display: block;
		background: rgba(0, 0, 0, 0.1);
		text-align: center;
		box-shadow: 6rpx 6rpx 12rpx #c5c5c5, -6rpx -6rpx 12rpx #ffffff;

		.names {
			font-size: 90%;
			font-weight: bolder;
			color: black;
			display: -webkit-box;
			display: -ms-flexbox;
			display: flex;
			line-height: 35rpx;

			p {
				font-weight: 800;
				float: left;
				line-height: 35rpx;
			}

			.left {
				opacity: 0.5;
				width: 50%;
				margin-top: 14rpx
			}

			.right {
				opacity: 0.5;
				width: 50%;
				margin-top: 14rpx
			}

			.css_sty {
				margin-top: 6rpx;
				background: rgb(244, 244, 244);
				// box-shadow: 6rpx 6rpx 11rpx #c5c5c5,
				// 	-6px -6rpx 11rpx #ffffff;
				position: absolute;
				width: 108rpx;
				height: 48rpx;
				background: #FFFFFF;
				border-radius: 28rpx;
			}
		}
	}
</style>